//js和jsx的交互接口
var cs = new CSInterface();
//创建按钮点击
/*
document.getElementById("btnCreate").addEventListener("click",function(){
    var width = document.getElementById("width").value;
    var height = document.getElementById("height").value;
    var resolution = document.getElementById("resolution").value;
    var docName = document.getElementById("docName").value;

    //执行main.jsx里面定义的creatNewDocument() 方法
    cs.evalScript("creatNewDocument("+width+","+height+","+resolution+",'"+docName+"')");
})
*/

function reSetAttrValue(attr, value) {
    //执行main.reSetAttrValue() 方法
    //alert(value);
    console.log(attr, value)
    cs.evalScript("reSetAttrValue('" + attr + "','" + value + "')");
    //cs.evalScript("creatNewDocument(\"100\", \"100\", \"100\", \"New Doc\")");
}

function onClickAttrValue(attr) {
    var value = attr;
    reSetAttrValue(attr, value)
}

function bindEventListener(elementId, callback) {
    var e = document.getElementById(elementId)
    if (e == null) {
        alert("elementId [" + elementId + "] is null")
        return
    }
    if (callback) {
        e.addEventListener("click", callback)
    }
}

bindEventListener("btn_9s", function() {
    var attr = "_9S:";
    var value = document.getElementById("input_scale9").value;
    var value = attr + value

    reSetAttrValue(attr, value)
})

bindEventListener("btn_9s_1",function(){
    var attr = "_9S:";
    var value = "";
    for (var i = 1; i <= 4; i++)
    {
        var name = "input_scale9_" + i;
        var v = document.getElementById(name).value;
        value += v;
        if (i != 4) value += ":";
    }
    var value = attr + value;
    reSetAttrValue(attr, value)
})

bindEventListener("btn_scrollview",function(){
    var attrs = [ "H", "V" ];
    var index = document.getElementById("select_scrollview").selectedIndex;
    var attr = "@ScrollView:" + attrs[index];
    var value = attr;
    reSetAttrValue(attr, value)
})

bindEventListener("btn_scrollbar",function(){
    var attr = "@ScrollBar:";
    var attrs = [ "L", "R", "T", "B" ];
    var index = document.getElementById("select_scrollbar").selectedIndex;
    var value2 = document.getElementById("input_scrollbar2").value;
    var value = attr + attrs[index] + ":" + value2;
    reSetAttrValue(attr, value)
})

bindEventListener("btn_slider",function(){
    var attrs = [ "L", "R", "T", "B" ];
    var index = document.getElementById("select_slider").selectedIndex;
    var attr = "@Slider:" + attrs[index];
    var value = attr;
    reSetAttrValue(attr, value)
})

bindEventListener("btn_tabgroup_tab",function(){
    var attr = "_Tab:";
    var value = document.getElementById("input_tabgroup_tab").value;
    var value = attr + value
    reSetAttrValue(attr, value)
})

bindEventListener("btn_group",function(){
    var attr = "@Group:";
    var attrs = [ "H", "V" ];
    var index = document.getElementById("select_group").selectedIndex;
    var value2 = document.getElementById("input_group2").value;
    var value = attr + attrs[index] + ":" + value2;
    reSetAttrValue(attr, value)
})

bindEventListener("btn_grid",function(){
    var attr = "@Grid:";
    var row = document.getElementById("input_grid1").value;
    var col = document.getElementById("input_grid2").value;
    var value = attr + row + ":" + col;
    reSetAttrValue(attr, value)
})